<?php


namespace app\command;

use app\common\lib\ZhyxBeanstalkd;
use app\common\lib\ZhyxMongo;
use app\common\model\TimingTask;
use catchAdmin\visitPlan\model\VisitPlan;
use MongoDB\BSON\ObjectId;
use thans\jwt\JWT;
use think\console\Command;
use think\console\Input;
use think\console\Output;


class VisitPlanPush extends Command
{
    protected function configure(): void
    {
        // 指令配置
        $this->setName('visitPlanPush')
            ->setDescription('复诊计划定时任务推送');
    }

    protected function execute(Input $input, Output $output)
    {
        $date = '2024-08-15';
        $taskList = (new TimingTask())->where([["task_plan_date", "=", $date], ['type', "=", 'visit_plan']])->select();
        if (!empty($taskList)) {
            $output->writeln("获取到跑批任务，即将投递到队列中");
            foreach ($taskList as $k => $v) {
                $output->writeln('开始投递任务:' . $v["task_data"]["task_name"] . "-" . $v["task_data"]["visit_id"]);
                $beanstalkdResult = ZhyxBeanstalkd::getInstance()->beansTalk->useTube("sendVisitMessage")->put(json_encode($v["task_data"], JSON_THROW_ON_ERROR), 0, 0);
                if (!empty($beanstalkdResult->getId())) {
                    ZhyxMongo::getInstance()->setCollection('visit_plan')->updateOne(["_id" => new ObjectId($v["task_data"]["visit_id"])], ['$set' => ["queue_id" => $beanstalkdResult->getId()]]);
                    $v->save(['task_status' => 1, 'task_oper_time' => date("Y-m-d H:i:s")]);
                }
            }
            $output->writeln('任务投递成功');

        } else {
            $output->writeln("当日没有需要投递的任务");
        }
    }
}